﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Interop;
using System.Runtime.InteropServices;
using System.Windows.Media.Animation;
using TinyCinema.UI.Pages;
using System.Threading.Tasks;

namespace TinyCinema.UI
{
    /// <summary>
    /// Interaction logic for Main.xaml
    /// </summary>
    public partial class Main : Window
    {
        #region Borderless Resizeable Control
        //phục vụ cho việc thay đổi kích thước cửa sổ
        private const int WM_SYSCOMMAND = 0x112;
        private HwndSource hwndSource;
        private enum ResizeDirection
        {
            Left = 61441,
            Right = 61442,
            Top = 61443,
            TopLeft = 61444,
            TopRight = 61445,
            Bottom = 61446,
            BottomLeft = 61447,
            BottomRight = 61448,
        }
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

        private void Main_SourceInitialized(object sender, EventArgs e)
        {
            hwndSource = PresentationSource.FromVisual((Visual)sender) as HwndSource;
        }
        private void ResizeWindow(ResizeDirection direction)
        {
            SendMessage(hwndSource.Handle, WM_SYSCOMMAND, (IntPtr)direction, IntPtr.Zero);
        }
        protected void Resize(object sender, MouseButtonEventArgs e)
        {
            var clickedShape = sender as Rectangle;

            switch (clickedShape.Uid)
            {
                case "ResizeN":
                    ResizeWindow(ResizeDirection.Top);
                    break;
                case "ResizeE":
                    ResizeWindow(ResizeDirection.Right);
                    break;
                case "ResizeS":
                    ResizeWindow(ResizeDirection.Bottom);
                    break;
                case "ResizeW":
                    ResizeWindow(ResizeDirection.Left);
                    break;
                case "ResizeNW":
                    ResizeWindow(ResizeDirection.TopLeft);
                    break;
                case "ResizeNE":
                    ResizeWindow(ResizeDirection.TopRight);
                    break;
                case "ResizeSE":
                    ResizeWindow(ResizeDirection.BottomRight);
                    break;
                case "ResizeSW":
                    ResizeWindow(ResizeDirection.BottomLeft);
                    break;
                default:
                    break;
            }
        }
        #endregion

        public static DependencyProperty ContentFrameSourceProperty = DependencyProperty.Register("pagContentFrameSource", typeof(Page), typeof(Window));

        public Page pagContentFrameSource
        {
            get { return (Page)GetValue(ContentFrameSourceProperty); }
            set { SetValue(ContentFrameSourceProperty, value); }
        }
        public Main()
        {
            SourceInitialized += Main_SourceInitialized;
            InitializeComponent();
            App.Current.MainWindow = this;            
        }
        
        bool IsMaximize = false;
        double _Width, _Height, _Left, _Top; 
        private void grdTopPanel_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ChangedButton == MouseButton.Left && e.ClickCount == 2)
                WindowState = (WindowState == WindowState.Normal) ? WindowState.Maximized : WindowState.Normal;
            else if (!IsMaximize) DragMove();
        }
        
        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            App.Current.Shutdown();
        }
        
        //Danh sách các page
        public static Pages.AdminPanel.MainPage pagAdminPanel = new Pages.AdminPanel.MainPage();
        public static Pages.Cinema.MainPage pagCinema = new Pages.Cinema.MainPage();
        public static Pages.Customer.MainPage pagCustomer = new Pages.Customer.MainPage();
        public static Pages.Dashboard.MainPage pagDashboard = new Pages.Dashboard.MainPage();
        public static Pages.Schedule.MainPage pagSchedule = new Pages.Schedule.MainPage();
        public static Pages.HumanResources.MainPage pagHumanResources = new Pages.HumanResources.MainPage();
        public static Pages.Movie.MainPage pagMovie = new Pages.Movie.MainPage();
        public static Pages.Menu.MainPage pagMenu = new Pages.Menu.MainPage();
        public static Pages.Promotion.MainPage pagPromotion = new Pages.Promotion.MainPage();
        public static Pages.Report.MainPage pagReport = new Pages.Report.MainPage();

        Button btnPreTab;
        bool IsTabControlMinimize = true;
        private void TabControl(object sender, RoutedEventArgs e)
        {
            if (sender is Window)
            {
                btnPreTab = TabList.Items[1] as Button;
                TabControl(btnPreTab, null);     
            }
            else
            {                
                var btnSender = sender as Button;
                switch (btnSender.Uid)
                {
                    case "AdminPanel": pagContentFrameSource = pagAdminPanel;
                        break;
                    case "Customer": pagContentFrameSource = pagCustomer;
                        break;
                    case "Dashboard": pagContentFrameSource = pagDashboard;
                        break;
                    case "Schedule": pagContentFrameSource = pagSchedule;
                        break;
                    case "HumanResources": pagContentFrameSource = pagHumanResources;
                        break;
                    case "Movie": pagContentFrameSource = pagMovie;
                        break;
                    case "Cinema": pagContentFrameSource = pagCinema;
                        break;
                    case "Menu": pagContentFrameSource = pagMenu;
                        break;
                    case "Promotion": pagContentFrameSource = pagPromotion;
                        break;
                    case "Report": pagContentFrameSource = pagReport;
                        break;
                    case "DoNothing": return;
                    case "Resize": if (IsTabControlMinimize)
                        {
                            (FindResource("stbResizeButtonAngle1") as Storyboard).Begin();
                            (FindResource("stbTablist1") as Storyboard).Begin();
                            IsTabControlMinimize = false;
                        }
                        else
                        {
                            (FindResource("stbResizeButtonAngle2") as Storyboard).Begin();
                            (FindResource("stbTablist2") as Storyboard).Begin();
                            IsTabControlMinimize = true;
                        }
                        return;
                    default: return;
                }
                btnPreTab.IsEnabled = true;
                btnSender.IsEnabled = false;
                btnPreTab = sender as Button;
            }

        }

        private void Window_StateChanged(object sender, EventArgs e)
        {
            if (WindowState == WindowState.Normal||WindowState == WindowState.Minimized) return;
            if (!IsMaximize)
            {
                WindowState = WindowState.Normal;
                bdrRoot.Margin = new Thickness(0);
                grdResize.Visibility = Visibility.Hidden;
                _Top = Top;
                _Left = Left;
                _Width = ActualWidth;
                _Height = ActualHeight;
                Top = Left = 0;
                Height = SystemParameters.MaximizedPrimaryScreenHeight - 7;
                Width = SystemParameters.MaximizedPrimaryScreenWidth;
                IsMaximize = true;
            }
            else
            {
                WindowState = WindowState.Normal;
                Top = _Top;
                Left = _Left;
                Height = _Height;
                Width = _Width;
                bdrRoot.Margin = new Thickness(7);
                grdResize.Visibility = Visibility.Visible;
                IsMaximize = false;
            }
        }

        private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (!IsMaximize) try
            {
                Opacity = 0.8;
                DragMove();                
            }                
            catch { }
            Opacity = 1;
        }

        private void WindowControl(object sender, RoutedEventArgs e)
        {
            var btnSender = sender as Button;
            switch(btnSender.Uid)
            {
                case "Min": this.WindowState = WindowState.Minimized;
                    break;
                case "Max": this.WindowState = WindowState.Maximized;
                    break;
                case "Close": App.Current.Shutdown();
                    break;
            }
        }

    }
}
